﻿using MettingOrder.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace MettingOrder.Controllers
{
    public class DePartMentController : BaseApiController
    {
        [HttpGet]
        public Result GetList()
        {
            return JsonHelper.Success(db.Query<Department>("where 1=1"));
        }
        /// <summary>
        /// 从微信中导入数据
        /// </summary>
        /// <returns></returns>
        public Result Import()
        {
            var departmentList = Common.DepartmentApi.Department.getAllDepartment();
            if (departmentList.department != null)
            {
                using (var tran = db.GetTransaction())
                {
                    db.Delete<Department>("where 1=1");
                    foreach (var item in departmentList.department)
                    {
                        Department department = new Department()
                        {
                            Id = item.id,
                            Name = item.name,
                            OrderId = item.order,
                            ParentId = item.parentid,
                        };
                        db.Insert(department);
                    }
                    tran.Complete();
                }
            }
            return JsonHelper.Success(departmentList);
        }
    }
}